<?php
class Pages extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('users_model');
        $this->load->helper('url_helper');
    }
    public function _get($str){
        $val = !empty($_GET[$str]) ? $_GET[$str] : null;
        return $val;
    }
    public function _post($str){
        $val = !empty($_POST[$str]) ? $_POST[$str] : null;
        return $val;
    }

      public function view($page = 'home')
    {
      if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
      {
          // Whoops, we don't have a page for that!
          show_404();
      }

      $data['title'] = ucfirst($page); // Capitalize the first letter

      $this->load->view('templates/header', $data);
      $this->load->view('pages/'.$page, $data);
      $this->load->view('templates/footer', $data);
    }
    public function login($slug = NULL)
    {
      $name = $this->_get('username') ? : $_POST['username'];
      $password = $this->_get('password') ? : $_POST['password'];
      $res['data'] = $this->users_model->get_users($name);
      if ($res['data'] == null) {
        $res['msg'] = '账号不存在';
        $res['status'] = -10001;
      } else {
        foreach ($res['data'] as $key => $value) {
          if ($key == 'password') {
            $res['status'] = $password == $value ? 200 : -10002;
            $res['msg'] = $password == $value ? '登陆成功' : '密码错误';
            $res['data'] = $password == $value ? $res['data'] : null;
          }
        }
      }

      $message = $this->users_model->resultData($res['data'],$res['status'],$res['msg']);
      if ($slug != NULL) {
          return $message;
      }
      echo json_encode($message);
    }

    public function if_show() {
        $slug = $this->_get('slug') ? : $_POST['slug'];
        $res['data'] = $this->users_model->get_users($slug);
        if ($res['data'] == null)  {
            $message = $this->users_model->resultData(null,-10001,'您没有操作权限');
            echo json_encode($message);
            return -10001;
        } else {
            return 200;
        }
    }
    public function register($slug = NULL)
    {
      $res['data'] = $this->users_model->get_users();
      $res['status'] = 200;
      foreach ($res['data'] as $index => $arr) {
        foreach ($arr as $key => $value) {
          if ($key == 'username') {
            if ($_POST['username'] == $value) {
              $message = $this->users_model->resultData(null,-10002,'该用户名已注册');
              echo json_encode($message);
              return;
            }
          }
        }
      }
       $this->users_model->register();
       $res['data'] = $this->users_model->get_users($_POST['username']);
       $message = $this->users_model->resultData($res['data'],200,'注册成功');
       echo json_encode($message);
    }

    public function if_reset($list,$title){
        $res['data'] = $this->users_model->getList($list);
        foreach ($res['data'] as $item) {
            foreach ($item as $key => $value) {
                if ($key == 'title') {
                    if ($title == $value) {
                        return 500;
                    }
                }
            }
        }
    }

    public function set_list()
    {
        if ($this->login('if_set')['status'] == -10001) {
            $message = $this->users_model->resultData(null,-10001,'您没有操作权限');
            echo json_encode($message);
            return;
        }
        $type = $_POST['type'];
        $res['data'] = $this->users_model->get_list($type);
        $title = $_POST['title'];
        $name = $_POST['name'];
        foreach ($res['data'] as $item) {
            foreach ($item as $key => $value) {
                if ($key == 'title') {
                    if ($title == $value) {
                        $message = $this->users_model->resultData(null,-10002,'此项已经添加，请勿重复添加');
                        echo json_encode($message);
                        return;
                    }
                }
            }
        }
        $this->users_model->set_list($type);
        $message = $this->users_model->resultData(null,200,'添加成功');
        echo json_encode($message);
    }

    public function get_list()
    {
        if ($this->if_show() == -10001) {
            return;
        }
        $res['data'] = $this->users_model->get_list();
        echo json_encode($res['data']);
    }

    public function get_nums()
    {
        if ($this->if_show() == -10001) {
            return;
        }
        $res['data'] = $this->users_model->getNums();
        echo json_encode($res['data']);
    }

    public function get_msg($slug = NULL)
    {
        if ($this->if_show() == -10001) {
            return;
        }
        $list_name = $this->_get('list_name');
        $msg = $this->users_model->getList($list_name);
        if ($msg != 500) {
            if ($slug != NULL) {
                return $msg;
            }
            $message = $this->users_model->resultData($msg);
            echo json_encode($message);
        } else {
            $message = $this->users_model->resultData(null,500,'没有查询到相关信息');
            echo json_encode($message);
        }
    }

    public function set_msg()
    {
        if ($this->if_show() == -10001) {
            return;
        }
        $data['list_name'] = $_POST['list_name'];
        $data['data'] = json_decode($_POST['data'],true);
        if ($this->if_reset($data['list_name'],$data['data']['title']) == 500) {
            $message = $this->users_model->resultData(null,-10002,'此项已经添加，请勿重复添加');
            echo json_encode($message);
            return;
        } else {
            $this->users_model->setList($data['list_name'],$data['data']);
            $message = $this->users_model->resultData(null,200,'添加成功');
            echo json_encode($message);
        }
    }

    public function multi_array_sort($multi_array, $sort_key, $sort = SORT_DESC)
    {
        if (is_array($multi_array)) {
            foreach ($multi_array as $row_array) {
                if (is_array($row_array)) {
                    $key_array[] = $row_array[$sort_key];
                } else {
                    return FALSE;
                }
            }
        } else {
            return FALSE;
        }
        array_multisort($key_array, $sort, $multi_array);
        return $multi_array;
    }

    public function get_products()
    {
        if ($this->if_show() == -10001) {
            return;
        }
        $type = $this->_get('type');
        $realType = strstr($type,'date') ? 'date' : 'heat';
        $data = $this->get_msg('slug');
        $data = $this->multi_array_sort($data,empty($type) ? 'id' : $realType,empty($type) ? SORT_ASC : SORT_DESC);
        $array = array();
        foreach($data as $value)
        {
            $newArray = array(
                "id" => $value['id'],
                "title" => $value['title'],
                "introduction" => $value['introduction'],
                "price" => $value['price'],
                "heat" => $value['heat'],
                "date" => $value['date']
            );
            array_push($array,$newArray);
        }
        switch($type)
        {
            case 'dateNew':
                echo json_encode(array_slice($array, 0, 3));
                break;
            case 'date':
                echo json_encode(array_slice($array, 3, 8));
                break;
            case 'heatNew':
                echo json_encode(array_slice($array, 0, 3));
                break;
            case 'heat':
                echo json_encode(array_slice($array, 3, 8));
                break;
            default :
                echo json_encode($array);
                break;
        }
    }

    public function get_item()
    {
        if ($this->if_show() == -10001) {
            return;
        }
        $type = $this->_get('list_name');
        $id = $this->_get('id');
        $data = $this->users_model->getItem($id,$type);
        echo json_encode($data);
    }

    public function delete_item()
    {
        if ($this->if_show() == -10001) {
            return;
        }
        $type = $_POST['list_name'];
        $id = $_POST['id'];
        $status = $this->users_model->deleteItem($id,$type);
        $data = $status == 200 ? $this->users_model->resultData(null,200,'删除成功') : 'fail';
        echo json_encode($data);
    }
      public function update_item()
      {
          if ($this->if_show() == -10001) {
              return;
          }
          $type = $_POST['list_name'];
          $id = $_POST['id'];
          $data = $this->users_model->getItem($id,$type);
          foreach ($data as $key => $value) {
              if ($key != 'id') {
                  $data[$key] = $this->_post($key) ? : $data[$key];
              }
          }
          $status = $this->users_model->updateItem($id,$type,$data);
          $data = $status == 200 ? $this->users_model->resultData(null,200,'修改成功') : 'fail';
          echo json_encode($data);
      }

      public function check_sms()
      {
          $code = $this->_get('code');
          $data = $code == 1314 ? $this->users_model->resultData(null,200,'验证码正确') : $this->users_model->resultData(null,-10001,'验证码错误');
          echo json_encode($data);
      }
  }

 ?>
